package com.stylefeng.guns.modular.map.controller;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.modular.area.dao.BdapAreaMapper;
import com.stylefeng.guns.modular.area.dao.BdapSubAreaMapper;
import com.stylefeng.guns.modular.area.model.BdapArea;
import com.stylefeng.guns.modular.area.model.BdapSubArea;
import com.stylefeng.guns.modular.heatmap.dao.HeatmapDao;
import com.stylefeng.guns.modular.heatmap.model.CellUc;
import com.stylefeng.guns.modular.util.MapUtil;

/**
 * 区域热力图控制器
 *
 * @author polly
 * @Date 2018年5月16日10:49:43
 */
@Controller
@RequestMapping("/map")
public class MapController extends BaseController {

	private String PREFIX = "/map/map/";

	@Autowired
	public HeatmapDao heatMapDao;

	@Autowired
	public BdapAreaMapper areaDao;

	@Autowired
	public BdapSubAreaMapper subAreaDao;

	/**
	 * 跳转到区域热力图首页
	 */
	@RequestMapping("")
	public String index(Model model) {
		model.addAttribute("time", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
		System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
		return PREFIX + "map.html";
	}
	
	/**
	 * 获取区域热力图列表
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public Object list(String condition) {
		return null;
	}

	/**
	 * 新增区域热力图
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Object add() {
		return super.SUCCESS_TIP;
	}

	/**
	 * 删除区域热力图
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Object delete() {
		return SUCCESS_TIP;
	}

	/**
	 * 修改区域热力图
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public Object update() {
		return super.SUCCESS_TIP;
	}

	/**
	 * 区域热力图详情
	 */
	@RequestMapping(value = "/detail")
	@ResponseBody
	public Object detail() {
		return null;
	}

	/**
	 * 区域热力图点击查询 根据查询条件，返回热力图所需数据 。查询条件：区域ID，日期，时间粒度
	 */
	@RequestMapping(value = "/search")
	@ResponseBody
	public Object search(@RequestParam(required = true) String time, @RequestParam(required = true) String granularity, @RequestParam(required = true) String areaId) {
		// 返回经度，维度，数值
		// 如果granularity是60分钟，查询小时表
		// 如果granularity是15分钟，查询15分钟表
		List<CellUc> cellUclist = Lists.newArrayList();
		String queryTime = LocalDate.parse(time).format(DateTimeFormatter.ofPattern("yyyyMMdd")).toString();
		if (granularity.equals(CellUc.HOUR_TIME)) {
			cellUclist = heatMapDao.hourSearch(queryTime, areaId);
		} else {
			cellUclist = heatMapDao.quarterSearch(queryTime, areaId);
		}
		// 经纬度偏移计算
		List<CellUc> newUcList = new ArrayList<CellUc>();// 偏移后的基站经纬度
		for (CellUc cc : cellUclist) {
			cc.setLng(Double.parseDouble(MapUtil.offset(cc.getLng() + "", "0.01185")));
			cc.setLat(Double.parseDouble(MapUtil.offset(cc.getLat() + "", "0.00328")));
			newUcList.add(cc);
		}

		// 查询区域的定位点，区域范围
		BdapArea area = areaDao.selectById(areaId);
		EntityWrapper<BdapSubArea> subAreaWrapper = new EntityWrapper<BdapSubArea>();
		subAreaWrapper.eq("PID", areaId);
		List<BdapSubArea> subareaList = subAreaDao.selectList(subAreaWrapper);
		JSONObject object = new JSONObject();
		object.put("cellUcList", newUcList);// 各个基站经纬度人数
		LinkedHashSet<String> timeSet = Sets.newLinkedHashSet();
		// 当天数据
		if (time.equals(LocalDate.now().toString())) {
			if (!cellUclist.isEmpty()) {
				if (granularity.equals(CellUc.HOUR_TIME)) {
					cellUclist.forEach(cu -> timeSet.add(LocalDateTime.parse(cu.time + "", DateTimeFormatter.ofPattern("yyyyMMddHH")).getHour() + ":00"));
				} else {
					cellUclist.forEach(cu -> timeSet.add(LocalDateTime.parse(cu.time + "", DateTimeFormatter.ofPattern("yyyyMMddHHmm")).getHour() + ":" + LocalDateTime.parse(cu.time + "", DateTimeFormatter.ofPattern("yyyyMMddHHmm")).getMinute()));
				}

			} else {
				for (int i = 0; i < LocalDateTime.now().getHour(); i++) {
					if (i < 10) {
						if (granularity.equals(CellUc.HOUR_TIME)) {
							timeSet.add("0" + i + ":" + "00");
						} else {
							timeSet.add("0" + i + ":" + "00");
							timeSet.add("0" + i + ":" + "15");
							timeSet.add("0" + i + ":" + "30");
							timeSet.add("0" + i + ":" + "45");
						}
					} else {
						if (granularity.equals(CellUc.HOUR_TIME)) {
							timeSet.add(i + ":" + "00");
						} else {
							timeSet.add(i + ":" + "00");
							timeSet.add(i + ":" + "15");
							timeSet.add(i + ":" + "30");
							timeSet.add(i + ":" + "45");
						}
					}
				}
			}
		} else {
			for (int i = 0; i < 24; i++) {
				if (i < 10) {
					if (granularity.equals(CellUc.HOUR_TIME)) {
						timeSet.add("0" + i + ":" + "00");
					} else {
						timeSet.add("0" + i + ":" + "00");
						timeSet.add("0" + i + ":" + "15");
						timeSet.add("0" + i + ":" + "30");
						timeSet.add("0" + i + ":" + "45");
					}
				} else {
					if (granularity.equals(CellUc.HOUR_TIME)) {
						timeSet.add(i + ":" + "00");
					} else {
						timeSet.add(i + ":" + "00");
						timeSet.add(i + ":" + "15");
						timeSet.add(i + ":" + "30");
						timeSet.add(i + ":" + "45");
					}

				}
			}
		}
		object.put("timeset", timeSet);// 数据时间集合
		int maxHour = Integer.parseInt(Collections.max(timeSet).split(":")[0]);
		object.put("maxHour", maxHour);
		object.put("areaInfo", area);// 区域经纬度
		object.put("subarea", subareaList);// 区域子区域经纬度
		return object;
	}
}
